package com.oren.system.ctrl;

import com.oren.base.ctrl.BaseController;
import com.oren.base.pojo.Page;
import com.oren.system.pojo.User;
import com.oren.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: oren
 * @Description:
 * @Date: Create on 2017/11/7 0007 09:33
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

	@Autowired
	private UserService userService;

	/**
	 * 根据用户id查询数据
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showUserById")
	public String toIndex(HttpServletRequest request, Model model)throws Exception{
		String  userName = request.getParameter("id");
		User user = userService.getUserByUserName(userName);
		model.addAttribute("user", user);
		return "showUser";
	}

	/**
	 * 查询用户的所有信息

	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showUserList")
	@ResponseBody
	public Page<User> list(User user,int page,int rows)throws Exception{
		if(user.getDepartmentId()!=null && user.getDepartmentId().equals("1")){
			user.setDepartmentId("");
		}
 		Page<User> Page=userService.pageList(user,page,rows);
		return  Page;
	}


	@RequestMapping("/toReg")
	public String toReg()throws Exception{
		return "reg";
	}

	//注册
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> addUser(User user)throws Exception{
		Map<String ,Object > map=new HashMap<String ,Object>();
		boolean f=false;
		User user1=userService.getUserByUserName(user.getUsername());
		if(user1!=null){
			map.put("status","false");
			map.put("msg","用户名已注册,请重新输入");
			return map;
		}else{
			f=userService.reg(user);
			if(f){
				map.put("status","success");
				map.put("msg","恭喜你，注册成功");
			}else{
				map.put("status","success");
				map.put("msg","不可控原因，注册失败,请稍后再试！");
			}
		}
		return map;
	}

	@RequestMapping("/home")
	public String toHome()throws Exception{
		return "system/home";
	}

	/**
	 * 登录
	 * @param username
	 * @param password
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login")
	@ResponseBody
	public Map login(String username,String password,HttpServletRequest request)throws Exception{
		User user =userService.login(username,password);
		Map map=new HashMap();
		if(user==null ){
			map= falseMessage("用户名或者密码错误");
		}else{
			map= successMessage("登录成功");
			HttpSession session=request.getSession();
			session.setAttribute("username",user.getUsername());
			session.setAttribute("userId",user.getId());
		}
		return map;
	}

	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Map delete(String ids){
		String [] idArray=ids.split(",");
		int i=0;
		try {
			 i=userService.deleteUserByIds(idArray);
		}catch (Exception e){
			e.printStackTrace();
		}
		if(i==0){
			return falseMessage("删除失败");
		}else{
			return successMessage("删除成功");
		}
	}

}

